package com.jn.exam.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jn.exam.JwtUtils;
import com.jn.exam.R;
import com.jn.exam.exam.entity.Student;
import com.jn.exam.exam.entity.StudentConnection;
import com.jn.exam.exam.entity.vo.ExamStudentVo;
import com.jn.exam.exam.service.ExamService;
import com.jn.exam.exam.service.StudentConnectionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author 江南大学1033190417
 * @date 2022/2/22 19:40
 */
@RestController
@RequestMapping("/exam/exam")
public class Exam2Controller {

    @Autowired
    private StudentConnectionService studentConnectionService;

    @Autowired
    private ExamService examService;



    @ApiOperation("获取学生参加考试的所有关联信息，学生端")
    @GetMapping("getExamStudentConnection")
    public R getExamStudentConnection(String examId, HttpServletRequest request) {
        String studentId = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<StudentConnection> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).eq("student_id", studentId);
        StudentConnection connection = studentConnectionService.getOne(wrapper);
        return R.ok().data("connection", connection);
    }


    @ApiOperation("获取学生参加考试的所有关联信息，教师端")
    @GetMapping("getExamStudentConnectionTea")
    public R getExamStudentConnectionTea(String examId,String studentId){
        QueryWrapper<StudentConnection> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).eq("student_id", studentId);
        StudentConnection connection = studentConnectionService.getOne(wrapper);
        return R.ok().data("connection", connection);
    }

    @ApiOperation("获取学生参加考试的所有关联信息，用于服务调用")
    @GetMapping("getExamStudentConnectionTeaForFeign/{examId}/{studentId}")
    public StudentConnection getExamStudentConnectionTeaForFeign(@PathVariable("examId") String examId,@PathVariable("studentId") String studentId){
        QueryWrapper<StudentConnection> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId).eq("student_id", studentId);
        return studentConnectionService.getOne(wrapper);
    }


    /**
     * 是否作弊  0/1
     * 阅卷情况：3种  -1/0/1
     * 参加情况：3中  -1/0/1
     *
     * @param examStudentVo 条件
     * @return 学生列表
     *
     *
     */
    @ApiOperation("查询参加某场考试的所有学生")
    @PostMapping("getAllStudentForExam")
    public R getAllStudentForExam(@RequestBody ExamStudentVo examStudentVo) {
        Integer total = examService.getPageStudentForExamTotal(examStudentVo);
        if (total == 0) {
            return R.ok().data("total", total).data("rows", null);
        }
        long current = examStudentVo.getCurrent();
        long limit = examStudentVo.getLimit();
        examStudentVo.setCurrent(current * limit);
        List<Student> students = examService.getPageStudentForExam(examStudentVo);
        return R.ok().data("total", total).data("rows", students);
    }

    @ApiOperation("更新学生参加考试的所有关联信息学生端")
    @PostMapping("updateExamStudentConnectionFromStu")
    public R updateExamStudentConnectionFromStu(@RequestBody StudentConnection studentConnection, HttpServletRequest request) {
        String studentId=JwtUtils.getMemberIdByJwtToken(request);
        UpdateWrapper<StudentConnection> wrapper = new UpdateWrapper<>();
        wrapper.eq("exam_id", studentConnection.getExamId()).eq("student_id", studentId);
        boolean isOK = studentConnectionService.update(studentConnection, wrapper);
        if (isOK) {
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation("更新学生参加考试的所有关联信息教师端")
    @PostMapping("updateExamStudentConnectionFromTeacher")
    public R updateExamStudentConnectionFromTeacher(@RequestBody StudentConnection studentConnection) {
        UpdateWrapper<StudentConnection> wrapper = new UpdateWrapper<>();
        wrapper.eq("exam_id", studentConnection.getExamId()).eq("student_id", studentConnection.getStudentId());
        boolean isOK = studentConnectionService.update(studentConnection, wrapper);
        if (isOK) {
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation("更新学生参加考试的所有关联信息用于服务调用")
    @PostMapping("updateExamStudentConnectionForFeign")
    public boolean updateExamStudentConnectionForFeign(@RequestBody StudentConnection studentConnection) {
        UpdateWrapper<StudentConnection> wrapper = new UpdateWrapper<>();
        wrapper.eq("exam_id", studentConnection.getExamId()).eq("student_id", studentConnection.getStudentId());
        return studentConnectionService.update(studentConnection, wrapper);
    }


   }
